﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NewBee.Core.BusinessService;

// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace WebMvc_Survey.ApiControllers
{
    /// <summary>
    /// 分享
    /// </summary>
    [Route("api/[controller]")]
    public class Survey_ShareController : Controller
    {
        AgentFactory _AgentFactory = new AgentFactory();

        /// <summary>
        /// 获取分享信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public NewBee.Core.Models.Survey_Share Get([FromQuery]string userId)
        {
            return _AgentFactory.Survey_Share.GetByUserId(userId);
        }

        /// <summary>
        /// 获取当前排名
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet, Route("GetNum")]
        public int GetNum([FromQuery]string userId)
        {
            NewBee.Core.Models.Survey_Share _Survey_Share = _AgentFactory.Survey_Share.GetByUserId(userId);

            return _AgentFactory.Survey_Share.Count(" ResultNum > @ResultNum", new { ResultNum = _Survey_Share.ResultNum }) + 1;
        }

        /// <summary>
        /// 获取排名分页列表
        /// </summary>
        /// <param name="pageParam"></param>
        /// <returns></returns>
        [HttpGet, Route("GetPageList")]
        public NewBee.Core.Data.Model.PageResult GetPageList(NewBee.Core.Data.Model.PageParam pageParam)
        {
            pageParam.sort = "ResultNum";
            return _AgentFactory.Survey_Share.GetPageList(pageParam);
        }

        //[HttpPut]
        //public bool Put([FromQuery]string id,[FromQuery]int num)
        //{
        //    return _AgentFactory.Survey_Share.Update(id, num);
        //}

        ///// <summary>
        ///// 新建一个分享账户
        ///// </summary>
        ///// <param name="survey_Share"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public NewBee.Core.Data.Model.SuccessResult Post([FromBody]NewBee.Core.Models.Survey_Share survey_Share)
        //{
        //    NewBee.Core.Data.Model.SuccessResult _SuccessResult = new NewBee.Core.Data.Model.SuccessResult();
        //    try
        //    {
        //        if (Get(survey_Share.UserID) == null)
        //            _SuccessResult.Success = _AgentFactory.Survey_Share.Insert(survey_Share);
        //        else
        //            _SuccessResult.Message = "用户已经存在";
        //    }
        //    catch(Exception ex)
        //    {
        //        _SuccessResult.Message = ex.Message;
        //    }
        //    _SuccessResult.EndTime = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString();
        //    return _SuccessResult;
        //}
    }
}
